package com.lucien.admin.controller;

import com.lucien.common.result.GlobalResult;
import com.lucien.common.utils.StringUtils;
import com.lucien.framework.web.login.SysLoginService;
import com.lucien.common.fornt.UserFront;
import com.lucien.system.domain.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: Lucien
 * @Description: 登录接口
 * @Date: Created in 2021/5/2
 */
@RestController
@Api(tags = "登录接口")
public class SysLoginController {

    @Autowired
    private SysLoginService loginService;

    @PostMapping("/login")
    @ApiOperation(value = "用户登录")
    public GlobalResult login(@RequestParam(name = "username", required = true) String username,
                              @RequestParam(name = "password", required = true)String password){
        UserFront result = loginService.login(username.trim(), password);
        if (StringUtils.equals(result.getMessage(), "登录成功")){
            return GlobalResult.success(result);
        }
        return GlobalResult.error("登录失败", result.getMessage());

    }

    @GetMapping("/logout")
    @ApiOperation(value = "用户注销")
    public GlobalResult logout(String token){
        String result = loginService.logout(token);
        return GlobalResult.success(result);
    }

    @GetMapping("/getloginuser")
    @ApiOperation(value = "获取登录用户信息")
    public GlobalResult getLoginUser(String token){
        UserFront result = loginService.getLoginUser(token);
        return GlobalResult.success(result);
    }

    @GetMapping("/getuser")
    @ApiOperation(value = "shiro获取当前用户")
    public GlobalResult getUser(){
        SysUser result = (SysUser) SecurityUtils.getSubject().getPrincipal();
        if (StringUtils.isNotNull(result)){
            return GlobalResult.success(result);
        }
        return GlobalResult.error("获取失败");
    }
}
